<template>
  <div class="common-layout">
    <el-container v-if="!isLoginweb">
      <el-aside width="160px"><HomeAside /></el-aside>
      <el-container>
        <el-header class="el-header"><HomeHeader /></el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>

    <Login v-if="isLoginweb" />
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, reactive, provide, inject } from "vue";
import HomeAside from "@/views/home/HomeAside.vue";
import HomeHeader from "@/views/home/HomeHeader.vue";
import Login from "@/views/use/login/Index.vue";

import { saveAllAccountlist } from "@/api/account/index.ts";
import { saveCookies } from "@/api/lutiancookie/index.ts";

const isLoginweb = inject('isLoginweb')
const activeuser = ref<any>({});
const accounts = ref([]);

const allunreadnum = ref(0)
provide('allunreadnum', allunreadnum);
provide('accounts', accounts);

const getActiveAccount = () => {
  console.log(activeuser);
  accounts.value = [];
  if (activeuser.value.isadmin == 1 || activeuser.value.isadmin == 2) {
    let data = {
      keyword: "",
    };
    saveAllAccountlist(data).then((res) => {
      console.log(res);
      if (res.code == 200) {
        res.data.forEach((item) => {
          let a = {
            value: item.id,
            label: item.account,
            cookie: "",
          };
          accounts.value.push(a);
        });
        // console.log(accounts.value);
        getAccountCookies();
      }
    });
  } else {
    if (activeuser.value.accounts != null && activeuser.value.accounts != "") {
      let accountsarr = activeuser.value.accounts.split(",");
      let accountsnamearr = activeuser.value.accountsname.split(",");

      for (let i = 0; i < accountsarr.length; i++) {
        let a = {
          value: accountsarr[i],
          label: accountsnamearr[i],
          cookie: "",
        };
        accounts.value.push(a);
      }
      // console.log(accounts.value);
      getAccountCookies();
    }
  }
};

const getAccountCookies = () => {
  let accountsvalue = accounts.value.map((obj) => obj.value);
  let data = {
    userId: activeuser.value.userId,
    accountids: JSON.stringify(accountsvalue),
  };
  saveCookies(data).then((res) => {
    if (res.code == 200) {
      res.data.forEach((item, index) => {
        accounts.value.forEach((item2,index2) => {
          if (item.accountid == item2.value) {
            let match = item.cookie.match(/bid_rid=(\d+)/);
            // 如果找到匹配的内容
            if (match) {
              // 第一个捕获组（即(\d+)）中的内容就是连续的数字
              let numbers = match[1];
              item2.cookie = item.cookie;
              item2.bid_rid = numbers;
              item2.unreadnum = 0
            }
          }
        });
      });
      console.log(accounts.value);
    }
  });
};


onMounted(() => {
  const currentUserstr = localStorage.getItem("currentUserstr");
  activeuser.value = JSON.parse(currentUserstr);
  console.log(activeuser.value);
  
  // setTimeout(() => {
  //   getActiveAccount();
  // }, 2000); 
  
  
})

</script>

<style scoped>
.el-container {
  height: 100vh;
}
.el-header {
  padding: 0;
  height: auto;
}
</style>